package com.servlet;

import com.dao.*;
import com.google.gson.Gson;
import com.helper.Util;
import com.model.*;
import com.model.Class;
import com.model.vo.CurrentAnswerVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/correct")
public class CorrectSerlvet extends HttpServlet {

	protected void doGet(HttpServletRequest request, 
			  HttpServletResponse response)
			throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String act = request.getParameter("act");
		AnswerDao answerDao = new AnswerDao();
		ExamDao examDao = new ExamDao();
		QuestionDiscussDao questionDiscussDao = new QuestionDiscussDao();
		PaperDao paperDao = new PaperDao();
		UserDao userDao = new UserDao();

		boolean success = false;
		switch (act) {
			case "saveScore":
				break;
			case "getDiscussQuestion":
				String studentNumber = request.getParameter("StudentNumber");
				String examName = request.getParameter("ExamName");
				String queNum = request.getParameter("QueNum");
				Exam exam = examDao.getByName(examName);
				User user = userDao.getByNumber(studentNumber);
				Answer answer = answerDao.getByExamNumUser(exam.getId(), Integer.parseInt(queNum), user.getId());
				if(answer!=null) {
					CurrentAnswerVo currVo = CurrentAnswerVo.fromAnswer(answer);
					Gson gson = new Gson();
					String json = gson.toJson(currVo);
					response.setContentType("application/json");
					PrintWriter writer = response.getWriter();
					writer.print(json);
					writer.close();
				}

				break;
			default:
				break;	
		}
	}
	
	
	protected void doPost(HttpServletRequest request, 
			  HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}
}
